package cn.iocoder.yudao.module.erp.service.accountplanrecord;

import cn.iocoder.yudao.module.erp.dal.dataobject.customeraccount.CustomerAccountDO;
import cn.iocoder.yudao.module.erp.dal.mysql.customeraccount.CustomerAccountMapper;
import cn.iocoder.yudao.module.erp.enums.Constant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.poi.hpsf.Decimal;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.accountplanrecord.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.accountplanrecord.AccountPlanRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.accountplanrecord.AccountPlanRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 客户余额对应记录 Service 实现类
 *
 * @author mhc
 */
@Service
@Validated
public class AccountPlanRecordServiceImpl implements AccountPlanRecordService {

    @Resource
    private AccountPlanRecordMapper accountPlanRecordMapper;

    @Resource
    private CustomerAccountMapper customerAccountMapper;

    @Override
    public Long createAccountPlanRecord(AccountPlanRecordSaveReqVO createReqVO) {
        // 插入
        AccountPlanRecordDO accountPlanRecord = BeanUtils.toBean(createReqVO, AccountPlanRecordDO.class);
        accountPlanRecordMapper.insert(accountPlanRecord);
        // 返回
        return accountPlanRecord.getId();
    }

    @Override
    @Transactional
    public Long createAccountPlanRecord(AccountPlanRecordDO accountPlanRecordDO) {
        accountPlanRecordMapper.insert(accountPlanRecordDO);
      /*  //得到记录
        CustomerAccountDO customerAccountDO=customerAccountMapper.selectOne(CustomerAccountDO::getCustomerId,accountPlanRecordDO.getCustomerId());
        customerAccountDO.setNowCount(customerAccountDO.getNowCount().add(accountPlanRecordDO.getMoneyCount()));
        if(accountPlanRecordDO.getType().equals(Constant.AccountPlanRecord_TYPE.销售.getValue())){
            BigDecimal count=accountPlanRecordDO.getMoneyCount();
            //减负数
            customerAccountDO.setSaleCount(customerAccountDO.getSaleCount().subtract(accountPlanRecordDO.getMoneyCount()));
        }else if(accountPlanRecordDO.getType().equals(Constant.AccountPlanRecord_TYPE.采购.getValue())){
            BigDecimal count=accountPlanRecordDO.getMoneyCount();
            //减负数
            customerAccountDO.setBuyCount(customerAccountDO.getBuyCount().add(accountPlanRecordDO.getMoneyCount()));
        }

        customerAccountMapper.updateById(customerAccountDO);*/




        return null;
    }

    @Override
    public void updateAccountPlanRecord(AccountPlanRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateAccountPlanRecordExists(updateReqVO.getId());
        // 更新
        AccountPlanRecordDO updateObj = BeanUtils.toBean(updateReqVO, AccountPlanRecordDO.class);
        accountPlanRecordMapper.updateById(updateObj);



    }

    @Override
    @Transactional
    public void updateAccountPlanRecord(AccountPlanRecordDO ado,BigDecimal newCount) {
        //得到记录
        CustomerAccountDO customerAccountDO=customerAccountMapper.selectOne(CustomerAccountDO::getCustomerId,ado.getCustomerId());

        customerAccountDO.setNowCount(customerAccountDO.getNowCount().subtract(ado.getMoneyCount()).add(newCount));

        if(ado.getType().equals(Constant.AccountPlanRecord_TYPE.销售.getValue())){
            BigDecimal count=ado.getMoneyCount();
            //减负数
            customerAccountDO.setSaleCount(customerAccountDO.getSaleCount().add(ado.getMoneyCount()).subtract(newCount));
        }else if(ado.getType().equals(Constant.AccountPlanRecord_TYPE.采购.getValue())){
            BigDecimal count=ado.getMoneyCount();
            //减负数
            customerAccountDO.setBuyCount(customerAccountDO.getBuyCount().subtract(ado.getMoneyCount()).add(newCount));
        }
        customerAccountMapper.updateById(customerAccountDO);
        ado.setMoneyCount(newCount);
        accountPlanRecordMapper.updateById(ado);
    }

    @Override
    public void deleteAccountPlanRecord(Long id) {
        // 校验存在
        validateAccountPlanRecordExists(id);
        // 删除
        accountPlanRecordMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteAccountPlanRecord(AccountPlanRecordDO accountPlanRecordDO) {
        //删除记录

        //得到记录
        CustomerAccountDO customerAccountDO=customerAccountMapper.selectOne(CustomerAccountDO::getCustomerId,accountPlanRecordDO.getCustomerId());
        customerAccountDO.setNowCount(customerAccountDO.getNowCount().subtract(accountPlanRecordDO.getMoneyCount()));
        if(accountPlanRecordDO.getType().equals(Constant.AccountPlanRecord_TYPE.销售.getValue())){
            BigDecimal count=accountPlanRecordDO.getMoneyCount();
            //减负数
            customerAccountDO.setSaleCount(customerAccountDO.getBuyCount().subtract(count.abs()));
        }else if(accountPlanRecordDO.getType().equals(Constant.AccountPlanRecord_TYPE.采购.getValue())){
            BigDecimal count=accountPlanRecordDO.getMoneyCount();
            //减负数
            customerAccountDO.setBuyCount(customerAccountDO.getBuyCount().subtract(count.abs()));
        }

        customerAccountMapper.updateById(customerAccountDO);

        accountPlanRecordMapper.deleteById(accountPlanRecordDO.getId());




    }

    private void validateAccountPlanRecordExists(Long id) {
        if (accountPlanRecordMapper.selectById(id) == null) {
            throw exception(ACCOUNT_PLAN_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public AccountPlanRecordDO getAccountPlanRecord(Long id) {
        return accountPlanRecordMapper.selectById(id);
    }

    @Override
    public PageResult<AccountPlanRecordDO> getAccountPlanRecordPage(AccountPlanRecordPageReqVO pageReqVO) {
        return accountPlanRecordMapper.selectPage(pageReqVO);
    }

}